<?php
/**
 * Created by PhpStorm.
 * User: mf
 * Date: 2017/9/4
 * Time: 9:53
 */
namespace app\admin\logic;

use think\Cookie;
use think\Db;
use tool\Common;
use think\Session;

class User extends Base
{
    //商家申请入驻注册
    public function sellerApply()
    {
        $check = $this->check(__FUNCTION__);
        if ($check['code'] != 1) {
            return $check;
        }
        $this->data['username'] = trim($this->data['username']);
        $this->data['sellerName'] = trim($this->data['sellerName']);
        $userInfo = Db::name('user')->where('username', $this->data['username'])->whereOr('sellerName',$this->data['sellerName'])->find();
        if ($userInfo['username'] == $this->data['username']) {
            return Common::rm(-2, '此用户名太受欢迎,请更换一个');
        }
        if ($userInfo['sellerName'] == $this->data['sellerName']) {
            return Common::rm(-2, '公司名称已存在');
        }
        $isMobile = Common::isMobile($this->data['mobile']);
        if (!$isMobile) {
            return Common::rm(-1, '手机号码格式不正确');
        }
        $lock = Common::createStr();
        $ar['username'] = $this->data['username'];
        $ar['password'] = md5($this->data['password'].$lock);
        $ar['sellerName'] = $this->data['sellerName'];
        $ar['mobile'] = $this->data['mobile'];
        $ar['email'] = $this->data['email'];
        $ar['address'] = $this->data['address'];
        $ar['roleID'] = 1;
        $ar['lock'] = $lock;
        $ar['addtime'] = time();
        $res = Db::name('user')->insert($ar);
        if ($res) {
            return Common::rm(1, '您已注册成功，请前去登录');
        } else {
            return Common::rm(-2, '对不起，注册失败');
        }
    }

    //得到商家的注册信息
    public function getSellerMsg()
    {
        $roleID = $this->getRoleID();
        $userID = $this->getUserID();
        if ($roleID == 1 || $roleID == 2) {
            if ($this->data['status'] == 0) {
                $userInfo = Db::name('user')->where('id',$userID) ->field('id as sellerID,sellerName,address,mobile,email,addtime')->find();
            } else {
                $userInfo = Db::name('user')->where('id',$userID)
                    ->field('id as sellerID,sellerName,address,contactman,mobile,email,bankName,bankCardNum,
                    description,comment,certificate,logo,range,addtime')
                    ->find();
            }
        } else {
            if (!isset($this->data['sellerID']) || !$this->data['sellerID']) {
                return Common::rm(-2,'请选择商家');
            }
            $userInfo = Db::name('user')->where('id',$this->data['sellerID'])
                ->field('id as sellerID,sellerName,address,contactman,mobile,email,bankName,bankCardNum,
                description,comment,certificate,logo,range,addtime')
                ->find();
        }
        if (!$userInfo) {
            return Common::rm(-3,'商家信息为空');
        }
        $userInfo['addtime'] = date('Y-m-d H:i:s',$userInfo['addtime']);
        return Common::rm(1,'操作成功',[
            'userInfo' => $userInfo
        ]);
    }

    //商家完善信息
    public function addSellerMsg()
    {
        $roleID = $this->getRoleID();
        if ($roleID != 1 && $roleID != 2) {
            return Common::rm(-2,'对不起，您没有权限操作');
        }
        $check = $this->check(__FUNCTION__);
        if ($check['code'] != 1) {
            return $check;
        }
        $userID = $this->getUserID();
        $ar['contactman'] = $this->data['contactman'];
        $ar['bankName'] = $this->data['bankName'];
        $ar['bankCardNum'] = $this->data['bankCardNum'];
        $ar['description'] = $this->data['description'];
        $ar['comment'] = $this->data['comment'];
        $ar['certificate'] = $this->data['certificate'];
        $ar['range'] = $this->data['range'];
        $ar['logo'] = $this->data['logo'];
        $ar['status'] = 1;
        $res = Db::name('user')->where('id',$userID)->update($ar);
        if ($res) {
            return Common::rm(1, '您已提交审核');
        } else {
            return Common::rm(-2, '对不起，提交审核失败');
        }
    }

    //用户登录(status-1审核未通过0注册成功初始化状态1正在审核中2审核通过，允许登录)
    public function login()
    {
        $check = $this->check(__FUNCTION__);
        if ($check['code'] != 1) {
            return $check;
        }
        $this->data['username'] = trim($this->data['username']);
        $this->data['password'] = trim($this->data['password']);
        $userInfo = Db::name('user')->where('username',$this->data['username'])->find();
        if (!$userInfo) {
            return Common::rm(-2,'该用户不存在');
        }
        if (md5($this->data['password'].$userInfo['lock']) != $userInfo['password']) {
            return Common::rm(-2,'密码错误，请重新输入');
        }
        $ar = [
            'userID' => $userInfo['id'],
            'roleID' => $userInfo['roleID'],
            'username' => $userInfo['username'],
            'password' => $userInfo['password'],
            'sellerName' => $userInfo['sellerName'],
            'key' => md5($userInfo['id'].$userInfo['username'].$userInfo['password'].$userInfo['sellerName'])
        ];
        Cookie::set('userinfo',$ar);
        Session::set('userinfo',$ar);
        return Common::rm(1,'登录成功',[
            'roleID' => $userInfo['roleID'],
            'name' => $userInfo['sellerName'],
            'status' => $userInfo['status']
        ]);
    }

    //用户退出
    public function logout()
    {
        Cookie::delete('userinfo');
        return Common::rm(1,'退出成功');
    }

    //验证登录状态
    public function checkLogin()
    {
        $cookie = Cookie::get('userinfo');
        if(!$cookie) {
            Common::json(Common::rm(-2, '请您登录'));
        } else {
            if (md5($cookie['userID'].$cookie['username'].$cookie['password'].$cookie['sellerName']) === $cookie['key']) {
                return Common::rm(1, '验证通过');
            } else {
                Common::json(Common::rm(-2, '请您登录'));
            }
        }

    }

    //生成登录界面的验证码
    public function verify()
    {

    }

    //谦程网络添加支队和分队账户
    public function addTeam()
    {
        $roleID = $this->getRoleID();
        if ($roleID != 3) {
            return Common::rm(-2,'您没有权限操作');
        }
        $check = $this->check(__FUNCTION__);
        if ($check['code'] != 1) {
            return $check;
        }
        $userInfo = Db::name('user')->where('userName',$this->data['username'])->find();
        if ($userInfo) {
            return Common::rm(-2,'此用户名太受欢迎,请更换一个');
        }
        $lock = Common::createStr();
        if (isset($this->data['parentID']) && $this->data['parentID']) {
            $roleID = 4;
        } else {
            $roleID = 5;
        }
        $ar = [
            'username' => $this->data['username'],
            'password' => md5($this->data['password'].$lock),
            'sellerName' => $this->data['teamName'],
            'parentID' => $this->data['parentID'],
            'lock' => $lock,
            'status' => 1,
            'roleID' => $roleID,
            'addtime' => THINK_START_TIME
        ];
        $res = Db::name('user')->insert($ar);
        if ($res) {
            return Common::rm(1,'添加成功');
        }
        return Common::rm(-2,'添加失败');
    }

    //总队得到支队列表
    public function getSonTeamList()
    {
        $roleID = $this->getRoleID();
        $userID = $this->getUserID();
        if ($roleID != 5) {
            return Common::rm(-2,'您没有权限操作');
        }
        $sonTeamList = Db::name('user')->where('parentID',$userID)->select();
        if (!$sonTeamList) {
            return Common::rm(-3,'数据为空');
        }
        $ar = [];
        foreach ($sonTeamList as $key => $val) {
            $ar[$key]['teamID'] = $val['id'];
            $ar[$key]['teamName'] = $val['sellerName'];
        }
        return Common::rm(1,'操作成功',[
            'sonTeamList' => $ar
        ]);
    }
}
